package com.googlecode.dtools.typeinfo.work.impl;

import com.googlecode.dtools.typeinfo.beans.DmType;
import com.googlecode.dtools.typeinfo.beans.ObjectFactory;
import com.googlecode.dtools.typeinfo.work.FilesWorker;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: A_Reshetnikov
 * Date: 10.06.11
 * Time: 16:04
 * To change this template use File | Settings | File Templates.
 */
public class FilesWorkerImpl implements FilesWorker {




    public DmType readXml(String typeName) {

        String fileName = outputPath + typeName + ".xml";
        DmType unmarshalledObject = null;
        JAXBContext jc = null;
        try {
            jc = JAXBContext.newInstance("com.googlecode.dtools.typeinfo.beans");

            Unmarshaller u = jc.createUnmarshaller();

            JAXBElement<DmType> element = (JAXBElement<DmType>) u.unmarshal(new File(fileName));
            unmarshalledObject = element.getValue();
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return unmarshalledObject;
    }

    private String fileWithTypeNames;

    public String getOutputPath() {
        return outputPath;
    }

    private String outputPath;

    public List<String> readTypeNameListFromFileSystem() {

        ArrayList<String> list = new ArrayList<String>();
        Collection<File> col = FileUtils.listFiles(new File(outputPath), new String[]{"xml"}, false);
        for (File file :col){
            list.add(FilenameUtils.getBaseName(file.getAbsolutePath()));
        }
        return list;
    }

    public List<DmType> readDmTypesFromFileSystem() {
        List<DmType> list = new ArrayList<DmType>();
        List<String> listNames = readTypeNameListFromFileSystem();
        for (String name: listNames){
            list.add(readXml(name));
        }
        return list;
    }

    public void saveXml(DmType dmType) {
        JAXBElement element = new ObjectFactory().createDmType(dmType);
        try {
            // create a JAXBContext capable of handling classes generated into package
            javax.xml.bind.JAXBContext jaxbContext
                    = javax.xml.bind.JAXBContext.newInstance("com.googlecode.dtools.typeinfo.beans");

            // create a Marshaller and do marshal
            javax.xml.bind.Marshaller marshaller = jaxbContext.createMarshaller();

            marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            // marshaller.setProperty(Marshaller.JAXB_ENCODING, "windows-1251");
            // marshaller.marshal(element, System.out);
            marshaller.marshal(element, new java.io.FileOutputStream(outputPath + dmType.getName() + ".xml"));
        } catch (javax.xml.bind.JAXBException je) {
            je.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }


    public void setFileWithTypeNames(String fileWithTypeNames) {
        this.fileWithTypeNames = fileWithTypeNames;
    }

    public void setOutputPath(String outputPath) {
        this.outputPath = outputPath;
    }

}
